/**
 * @Copyright (c) 2007,上海晨路信息科技有限公司
 * @All rights reserved.
 *
 *
 *
 * @file_name   Tabs.js
 * @version     1.0
 * @author      黄新泽
 * @date        2007-12-03 09:36:09
 */

/**
 * Class and Function List:
 * Function list:
 * - initialize : function(element) 
 * - setupTab : function(elm)       
 * - activate :  function(ev)       
 * - hide : function(elm)           
 * - show : function(elm)           
 * - tabID : function(elm)          
 * - getInitialTab : function()     
 * - 
 * - 
 * Classes list:
 * - .Tabs
 */

var tabsInitFlag = false;
var currentTabId = false;	//当前显示item

var Tabs = Class.create();

Tabs.prototype = {
	initialize : function(element)
	{
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));

//		if ($(this.tabID(this.getInitialTab())).tree)
//		{
//			
//		}

//		if (this.getInitialTab().getAttribute('init'))
//		{
//			alert(this.tabID(this.getInitialTab()));
//		}
	},

	empty : function(){return false;},

	setupTab : function(elm)
	{
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
		elm.onclick = this.empty;
	},

	activate :  function(ev)
	{
		var elm = Event.findElement(ev, "a");
		elm.blur();

		this.activateStart(elm);
		
		ev = null;
	},

	activateStart :  function(elm)
	{
		elm = $(elm);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));

		try
		{
			//点击事件,加载数据
			var elm;

			var i = 0;
			while (i < 10)
			{
				elm = this.getChildActive(elm);

				if ('top' == elm.getAttribute('tree'))
				{
					break;
				}

				i++;
			}

		}
		catch(e)
		{
		}


		try
		{
			if ('top' == elm.getAttribute('tree'))
			{
				if ('undefined' != typeof(mask))
				{
					mask.deEvent(this.tabID(elm));
				}

				$(this.tabID(elm)).update('<table style="text-align:center;width:100%;height:380px;" ><tr><td  valign="middle"><img src="' + IMA_PATH + '/js/loading2.gif" border="0"></td></tr></table>');
				eval(this.tabID(elm) + '.sendRequest();');
				currentTabId = this.tabID(elm);
			}
		}
		catch(e)
		{
		}

		elm = null;
	},

	getChildActive :  function(elm)
	{
		//点击事件
		var menu = $(this.tabID(elm) + '_navi').getElementsByTagName('a');

		var return_elm = false;

		$A(menu).each(function(elm){
			if (elm.hasClassName('on'))
			{
				return_elm = elm;
			}
		});

		//解决丢失问题, 临时
		if (!return_elm)
		{
			return_elm = menu[0];
			return_elm.addClassName('on');
			this.show(return_elm)
		}

		menu = null;
		elm = null;

		return return_elm;
	},


	jump :  function(elm)
	{
		var item = elm.split('|');

		//alert(Object.toJSON(item));

		//根菜单处理
		var root = item[0];
		top_navi.showAndHideItem(root + '_a');

		var top = item.pop();

		var l = item.length - 1;

		for	(var i=0; i<l; i++)
		{
			var c = item[i] + '_navi';
			var a = item[i+1] + '_a';

			eval(c + '.showAndHideItem("' + a + '");');

			c = a = null;
		}

		//树梢菜单处理
		this.activateStart(top + '_a');

		item = null;
		root = null;
		top = i = c = a = null;
		return false;
	},


	showAndHideItem :  function(elm)
	{
		elm = $(elm);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},


	hide : function(elm)
	{
		$(elm).removeClassName('on');
		$(this.tabID(elm)).removeClassName('active-tab-body');

		try
		{
			if ('top' == $(elm).getAttribute('tree'))
			{
				if ('undefined' != typeof(mask))
				{
					mask.deEvent(this.tabID(elm));
				}

				$(this.tabID(elm)).update('<table style="text-align:center;width:100%;height:380px;" ><tr><td  valign="middle"><img src="' + IMA_PATH + '/js/loading2.gif" border="0"></td></tr></table>');
			}
		}
		catch(e)
		{
		}


	},

	show : function(elm)
	{

		try
		{
			if (!tabsInitFlag && 'on'==elm.getAttribute('init'))
			{
				tabsInitFlag = true;

				if ('undefined' != typeof(mask))
				{
					mask.deEvent(this.tabID(elm));
				}

				$(this.tabID(elm)).update('<table style="text-align:center;width:100%;height:380px;" ><tr><td  valign="middle"><img src="' + IMA_PATH + '/js/loading2.gif" border="0"></td></tr></table>');
				eval(this.tabID(elm) + '.sendRequest();');
				currentTabId = this.tabID(elm);
			}
		}
		catch(e)
		{
		}


		$(elm).addClassName('on');
		$(this.tabID(elm)).addClassName('active-tab-body');

//		if (elm.id == 'military_soldiers_a' || elm.id == 'military_science_a')
//		{
//			rRecource(1);
//		}
//		else
//		{
//			rRecource(0);
//		}
	},

	tabID : function(elm)
	{
		return elm.href.match(/#(\w.+)/)[1];
	},

	getInitialTab : function()
	{
		if(document.location.href.match(/#(\w.+)/))
		{
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} 
		else
		{
			return this.menu.first();
		}
	}
}
